package com.guomao.util;

import com.guomao.interfaces.Calculator;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

/**
 * @author wangzhen
 */
public class CalculatorProxy {
/*    public static Calculator getProxy(Calculator c) throws FileNotFoundException {

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\calc.log")));

        Class<? extends Calculator> clazz = c.getClass();
        // 1 被代理对象的类加载器
        ClassLoader loader = clazz.getClassLoader();
        //系统类 / 扩展类 / 应用类加载器
        // 2 被代理对象实现的所有接口
        Class<?>[] interfaces = clazz.getInterfaces();
        // 3 方法执行处理器 方法对象.invoke(目标obj,实际参数);
        InvocationHandler h = (proxy, method, args) -> {
            Object result = null;
            try {
//                System.out.println(method.getName() + "开始执行,参数是:" + Arrays.toString(args));
                Log.logStart(method, args);
//                bw.write(method.getName() + "开始执行,参数是:" + Arrays.toString(args));
//                bw.newLine();
                result = method.invoke(c, args);
//                System.out.println(10 / 0);
//                System.out.println(method.getName() + "结束执行,结果是:" + result);
//                bw.write(method.getName() + "结束执行,结果是:" + result);

                Log.logEnd(method, result);
//                bw.newLine();
            } catch (Exception e) {
//                System.out.println(method.getName() + "出现异常,异常是:" + e);
//                bw.write(method.getName() + "出现异常,异常是:" + e);
//                bw.newLine();
                Log.logException(method, e);
            } finally {
//                System.out.println(method.getName() + "最终结束");
//                bw.write(method.getName() + "最终结束");
//                bw.newLine();

                Log.logFinal(method);
            }

//            bw.flush();
//            bw.close();
            return result;
        };

        // 创建代理对象
        Object o = Proxy.newProxyInstance(loader, interfaces, h);
        return (Calculator) o;
    }*/


    public static Calculator getProxy(Calculator c) throws FileNotFoundException {

        Class<? extends Calculator> clazz = c.getClass();
        // 1 被代理对象的类加载器
        ClassLoader loader = clazz.getClassLoader();
        // 2 被代理对象实现的所有接口
        Class<?>[] interfaces = clazz.getInterfaces();
        // 3 方法执行处理器 方法对象.invoke(目标obj,实际参数);
        InvocationHandler h = (proxy, method, args) -> {
            Object result = null;
            try {
//                Log.logStart(method, args);
                result = method.invoke(c, args);
//                System.out.println(10/0);
//                Log.logEnd(method, result);
            } catch (Exception e) {
//                Log.logException(method, e);
            } finally {
//                Log.logFinal(method);
            }
            return result;
        };

        // 创建代理对象
        Object o = Proxy.newProxyInstance(loader, interfaces, h);
        return (Calculator) o;
    }


}
